---
title: "Can Rebuttals Restore Confidence -- Survey Experiments"
output: rmarkdown::null_document
---

```{r set-up}

library(pacman)

p_load(tidyverse, marginaleffects, ggplot2, 
       knitr, texreg, ggdist, ggpubr, ggtext, gmodels, 
       fixest, xtable, psych, rlang)


dat <- readRDS("cleaned_svy_dat.Rds")


```

# Main Effects

Models of confidence in the INE for Studies 1 and 2, and for the two studies combined. We include pre-registered covariates for vote intention, political knowledge, and pre-treatment trust in the INE. The combined model includes a fixed effect for Study 2, capturing differences between the studies.

Pre-treatment confidence in the INE is measured on a five-point-scale in Study 1 and ten-point-scale in Study 2. The pre-registration for Study 1 included feeling thermometers for AMLO and the main opposition candidate as covariates. We excluded these measures due to item-non-response.

```{r models}


study1_fit <- lm(conf_ine ~ tx + vote_intent + know_index + trust_ine_pre,
                 data = subset(dat, study == 1))

study2_fit <- lm(conf_ine ~ tx + vote_intent + know_index + trust_ine_pre, 
                 data = subset(dat, study == 2))

combined_fit <- lm(conf_ine ~ tx + vote_intent + know_index + 
                     trust_ine_pre + factor(study), data = dat)

```

```{r print models}


coef_list <- list( "(Intercept)" = "Baseline",
                   "txattack" = "Attack",
                   "txexpert" = "Expert Rebuttal",
                   "txpartisan" = "Partisan Rebuttal",
                   "txdomestic" = "Domestic Rebuttal",
                   "vote_intentNone/Other" = "Party Support: None",
                   "vote_intentXochitl" = "Party Support: PAN/PRI/PRD",
                   "trust_ine_pre" = "Trust INE", 
                   "know_index" = "Political Knowledge",
                   "factor(study)2" = "Study 2 FE"
                )

cap1 <- "OLS models of confidence in the Instituto Nacional Electoral."


texreg(list(study1_fit, study2_fit, combined_fit),
       custom.coef.map = coef_list,
       stars = c(0.1, 0.05, 0.01),
       caption = cap1,
       caption.above = T,
       dcolumn = T,
       file = "tables/main_models.tex",
       label = "si:main_models")


```

```{r function-for-specific-comparions}
#| echo: false

# quick function for comparisons
my_comparisons <- function(data, tx_levels) {
  avg_comparisons(data, 
                  variables = list(tx_char = tx_levels),
                  by = "vote_intent",
                  conf_level = c(.90)) 
}

```

## Plot Comparisons

### Hypotheses

We test the following hypotheses:

- The rhetorical attack condition will decrease confidence in the INE relative to the control condition.

- The domestic expert rebuttal condition will increase confidence in the INE relative to the rhetorical attack condition.

- The partisan rebuttal condition will increase confidence in the INE relative to the rhetorical attack condition.

- The international expert rebuttal will increase confidence in the INE relative to the rhetorical attack condition. Expert rebuttals will be more effective than partisan rebuttals.

- Confidence in the INE will be greater in the international expert rebuttal condition than in the partisan rebuttal condition.

- Confidence in the INE will be greater in the domestic expert rebuttal condition than in the partisan rebuttal condition

We need to restimate with vote intent as a character because `marginaleffects` gets cranky with missing factor levels. 

```{r restimate-with-tx_char}

study1_fit <- lm(conf_ine ~ tx_char + vote_intent + know_index + trust_ine_pre,
                 data = subset(dat, study == 1))

study2_fit <- lm(conf_ine ~ tx_char + vote_intent + know_index + trust_ine_pre, 
                 data = subset(dat, study == 2))


```


```{r main-comparisons}

# Function for comparisons between set levels
main_comparisons <- function(data, tx_levels) {
  avg_comparisons(data, 
                  variables = list(tx_char = tx_levels),
                  conf_level = c(.90)) 
}


# effects from study 1
effects1 <- rbind(
  main_comparisons(study1_fit, tx_levels = c("control", "attack")),
  main_comparisons(study1_fit, tx_levels = c("attack", "expert")),
  main_comparisons(study1_fit, tx_levels = c("attack", "partisan")),
  main_comparisons(study1_fit, tx_levels = c("partisan", "expert"))
  ) |>  
  mutate(study = "Study 1")



# effects from study 2
effects2 <- rbind(
  main_comparisons(study2_fit, tx_levels = c("control", "attack")),
  main_comparisons(study2_fit, tx_levels = c("attack", "expert")),
  main_comparisons(study2_fit, tx_levels = c("attack", "partisan")),
  main_comparisons(study2_fit, tx_levels = c("attack", "domestic")),
  main_comparisons(study2_fit, tx_levels = c("partisan", "expert")),
  main_comparisons(study2_fit, tx_levels = c("partisan", "domestic"))
  ) |>     
  mutate(study = "Study 2")


main_effects <- rbind(effects1, effects2) 

main_effects <- main_effects |>
  mutate(
    
    # get rid of mean() around study
    contrast = gsub("mean\\(|\\)", "", contrast), 
    
    # recode comparisons for plotting
    comparison = factor(
      case_match(contrast,
                 "attack - control" ~ "Attack",
                 "expert - attack" ~ "Intl. Expert",
                 "partisan - attack" ~ "Partisan",
                 "domestic - attack" ~ "Dom. Expert",
                 "expert - partisan" ~ "Intl. Expert",
                 "domestic - partisan" ~ "Dom. Expert"
                 ),
      levels = c("Attack", 
                 "Partisan",
                 "Dom. Expert",
                 "Intl. Expert")
      ),

    # label the study number
    study = factor(study, 
                   levels = c("Study 1", "Study 2")),
    
    # code the baseline
    baseline = factor(
      case_match(contrast, 
                 "attack - control" ~ "Relative to Control",
                 "expert - attack" ~ "Relative to Attack",
                 "partisan - attack" ~ "Relative to Attack",
                 "domestic - attack" ~ "Relative to Attack",
                 "expert - partisan" ~ "Relative to Partisan",
                 "domestic - partisan" ~ "Relative to Partisan"),
      levels = c("Relative to Control", 
                 "Relative to Attack",
                 "Relative to Partisan")
      )
    )

```

Figure shows the effect of the attack relative to the control (upper panel), the rebuttals relative to the attack (center panel), and the expert and domestic rebuttals relative to the partisan rebuttal (lower panel).

```{r plot-main-comparisons}

#plot
main_plot <- ggplot(main_effects, aes(x = estimate,
                          y = comparison,
                          xmin = conf.low,
                          xmax = conf.high,
                          color = study,
                          shape = study)) +
  labs(x = "Estimated Effect", y = "Condition") +
  geom_pointrange(position = position_dodge(width = .7)) +
  facet_wrap( ~ baseline, ncol = 1, scales = "free_y") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
   theme(
    strip.background =  element_rect(fill = "#F7F7F7", size = .25, color = "black"),
    panel.background = element_rect(fill = "white", color = "black", size = .25),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    #axis.title.y = element_text(face = "bold"),
    #axis.title.x = element_text(face = "bold"),
    strip.text = element_text(size = 10, hjust = 0.5, color = "black"), 
    plot.title = element_text(hjust = .5),
    axis.text.x = element_text(color = "black"),
    axis.text.y = element_text(color = "black", size = 10), 
    legend.position = "bottom", 
    legend.title = element_blank(),
    legend.background = element_blank(),
    legend.key = element_blank(),
    legend.box.background = element_blank(),
    panel.spacing = unit(.25, "lines")
  ) 

main_plot

ggsave("figures/main_effects_combined.pdf", 
       height = 4, width = 4)


```


## Print Main Comparisons

```{r comparisons-table}

# Study 1 Comparisons Table ###

# Add Comparisons we didn't plot
control_expert_s1 <- rbind(
  main_comparisons(study1_fit, tx_levels = c("control", "expert")),
  main_comparisons(study1_fit, tx_levels = c("control", "partisan"))
  ) |> 
  # add study and p-values
    mutate(study = "Study 1")

# Bind with existing comparisons and print
rbind(effects1, control_expert_s1) |> 
  # add one-tailed p-values
  mutate(
    lower_tail = pnorm(statistic, lower.tail = T),
    upper_tail = pnorm(statistic, lower.tail = F)
    ) |> 
  select(Comparison = contrast, 
         Estimate = estimate,
         "Z-Statistic" = statistic,
         "Two-Tailed" = p.value,
         "Lower Tail" = lower_tail,
         "Upper Tail" =  upper_tail) |>
  xtable(digits = 2) |> 
  print(include.rownames = FALSE, 
        file = "tables/comparisons_study1.tex")


# Study 2 Comparisons Table ###

# Add Comparisons we didn't plot
control_expert_s2 <- rbind(
  main_comparisons(study2_fit, tx_levels = c("control", "expert")),
  main_comparisons(study2_fit, tx_levels = c("control", "partisan")),
  main_comparisons(study2_fit, tx_levels = c("control", "domestic")),
  main_comparisons(study2_fit, tx_levels = c("expert", "domestic"))
  
  ) |> 
  # add study
    mutate(study = "Study 2")

# Bind with existing comparisons and print
rbind(effects2, control_expert_s2) |> 
    # add one-tailed p-values
  mutate(
    lower_tail = pnorm(statistic, lower.tail = T),
    upper_tail = pnorm(statistic, lower.tail = F)
    ) |> 
  select(Comparison = contrast, 
         Estimate = estimate,
         "Z-Statistic" = statistic,
         "Two-Tailed" = p.value,
         "Lower Tail" = lower_tail,
         "Upper Tail" =  upper_tail) |>
  xtable(digits = 2) |> 
  print(include.rownames = FALSE, 
        file = "tables/comparisons_study2.tex")



```


# Effects By Party Support

## Estimate and Print Models

```{r models-by-vote-intent}

# study 1 model
study1_inter <- lm(conf_ine ~ tx * vote_intent + know_index + trust_ine_pre,
                 data = subset(dat, study == 1))

# study 2 model
study2_inter <- lm(conf_ine ~ tx * vote_intent + know_index + trust_ine_pre, 
                 data = subset(dat, study == 2))

# studies combined
study_inter <- lm(conf_ine ~ tx * vote_intent + know_index + trust_ine_pre + factor(study),
                 data = dat)

```


```{r party-models-to-latex}


coef_list <- list( "(Intercept)" = "Baseline",
                   "txattack" = "Attack",
                   "txattack:vote_intentXochitl"  = "Attack x PAN/PRI/PRD",
                   "txattack:vote_intentNone/Other" = "Attack x None/Other",
                   "txexpert" = "Expert Rebuttal",
                   "txexpert:vote_intentXochitl" = "Expert x PAN/PRI/PRD",
                   "txexpert:vote_intentNone/Other" = "Expert x None/Other",
                   "txpartisan" = "Partisan Rebuttal",
                   "txpartisan:vote_intentXochitl" = "Partisan x PAN/PRI/PRD",
                   "txpartisan:vote_intentNone/Other" = "Partisan x None/Other",
                   "txdomestic" = "Domestic Rebuttal",
                   "txdomestic:vote_intentXochitl" = "Domestic x PAN/PRI/PRD",
                   "txdomestic:vote_intentNone/Other" = "Domestic x None/Other",
                   "vote_intentNone/Other" = "Party Support: None",
                   "vote_intentXochitl" = "Party Support: PAN/PRI/PRD",
                   "trust_ine_pre" = "Trust INE", 
                   "know_index" = "Political Knowledge",
                   "factor(study)2" = "Study 2 FE"
                )

cap1 <- "OLS models of confidence in the Instituto Nacional Electoral."


texreg(list(study1_inter, study2_inter, study_inter),
       custom.coef.map = coef_list,
       stars = c(0.1, 0.05, 0.01),
       caption = cap1,
       caption.above = T,
       dcolumn = T,
       file = "tables/inter_models.tex",
       label = "si:inter_models")


```


```{r restimate_models_with_char}

# study 1 model
study1_inter <- lm(conf_ine ~ tx_char * vote_intent + know_index + trust_ine_pre,
                 data = subset(dat, study == 1))

# study 2 model
study2_inter <- lm(conf_ine ~ tx_char * vote_intent + know_index + trust_ine_pre, 
                 data = subset(dat, study == 2))

# studies combined
study_inter <- lm(conf_ine ~ tx_char * vote_intent + know_index + trust_ine_pre + factor(study),
                 data = dat)

```

## Calculate Comparisons and Plot

```{r plot-effects-by-vote-intent}
# quick function for comparisons
my_comparisons <- function(data, tx_levels) {
  avg_comparisons(data, 
                  variables = list(tx_char = tx_levels), # compare tx levels
                  by = "vote_intent", # by vote intention
                  conf_level = c(.90))  # 90% CIS
}


# effects from study 1
cates_s1 <- rbind(
  my_comparisons(study1_inter, tx_levels = c("control", "attack")),
  my_comparisons(study1_inter, tx_levels = c("attack", "expert")),
  my_comparisons(study1_inter, tx_levels = c("attack", "partisan"))
) %>% mutate(study = "Study 1")

# effects from study 2
cates_s2 <- rbind(
  my_comparisons(study2_inter, tx_levels = c("control", "attack")),
  my_comparisons(study2_inter, tx_levels = c("attack", "expert")),
  my_comparisons(study2_inter, tx_levels = c("attack", "partisan")),
  my_comparisons(study2_inter, tx_levels = c("attack", "domestic"))
) %>% mutate(study = "Study 2")

# combined
cates_combined <- rbind(
  my_comparisons(study_inter, tx_levels = c("control", "attack")),
  my_comparisons(study_inter, tx_levels = c("attack", "expert")),
  my_comparisons(study_inter, tx_levels = c("attack", "partisan")) 
) |>  mutate(study = "Combined")



# clean up the dataframe of effects
inter_effects <- rbind(cates_s1, cates_s2, cates_combined) |>
  mutate(
    
    # get rid of mean() around study
    contrast = gsub("mean\\(|\\)", "", contrast), 
    
    # recode comparisons for plotting
    comparison = factor(
      case_match(contrast,
                 "attack - control" ~ "Control \nto Attack",
                 "expert - attack" ~ "Attack \nto Intl. Expert",
                 "partisan - attack" ~ "Attack \nto Partisan",
                 "domestic - attack" ~ "Attack \nto Dom. Expert"),
      levels = c("Control \nto Attack", 
                 "Attack \nto Intl. Expert", 
                 "Attack \nto Partisan",
                 "Attack \nto Dom. Expert")
      ),
    
    # labels for gov/opposition/other supporters
    support = factor(
      case_match(vote_intent, 
                 "Claudia" ~ "MORENA", 
                 "Xochitl" ~ "PAN/PRI/PRD",
                "None/Other" ~ "None/Other"),
      levels = c("None/Other",
                 "PAN/PRI/PRD",
                 "MORENA")
      ),
    
    # label the study number
    study = factor(study, 
                   levels = c("Study 1", "Study 2", "Combined"))
    
    )

# plot effects 
het_effects_plot <- ggplot(inter_effects, aes(x = estimate,
                          y = support,
                          xmin = conf.low,
                          xmax = conf.high,
                          color = study,
                          shape = study)) +
  geom_pointrange(position = position_dodge(width = .7)) +
  facet_wrap(~comparison) +
  labs(x = "Estimated Effect", y ="Party Support") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  theme(
    strip.background =  element_rect(fill = "#F7F7F7", size = .25, color = "black"),
    panel.background = element_rect(fill = "white", color = "black", size = .25),
    strip.text = element_text(size = 10, hjust = 0.5, color = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "bottom", 
    axis.text = element_text(color = "black", size = 10), 
    legend.title = element_blank(),
    legend.background = element_blank(),
    legend.key = element_blank()
    )

het_effects_plot

ggsave("figures/het_effects_combined.pdf", 
       height = 4, width = 4.2)



```

## Table of Comparisons by Party Support

```{r tables-comparisons-by-party}

# Comparisons for each study 
all_party_comparisons <- rbind(
   
  # comparisons study 1
   avg_comparisons(study1_inter, 
                  variables = list(tx_char = "pairwise"), 
                  by = "vote_intent") |> mutate(study = "1"),
   
  # comparisons study 2
  avg_comparisons(study2_inter, 
                  variables = list(tx_char = "pairwise"), 
                  by = "vote_intent") |> mutate(study = "2"),
  
  # comparisons combined 
   avg_comparisons(study_inter, 
                  variables = list(tx_char = "pairwise"), 
                  by = "vote_intent") |> mutate(study = "combined")
  ) 

all_party_comparisons <- all_party_comparisons |> 
  # recodes 
  mutate(
    # remove parentheses
    contrast = gsub("mean\\(|\\)", "", contrast), 
    # lower tail p-values
    lower_tail = pnorm(statistic, lower.tail = T),
    # upper tail ´p-values
    upper_tail = pnorm(statistic, lower.tail = F),
    # party support
    support = factor(
      case_match(vote_intent, 
                 "Claudia" ~ "MORENA", 
                 "Xochitl" ~ "PAN/PRI/PRD",
                "None/Other" ~ "None/Other"),
      levels = c("MORENA",
                 "PAN/PRI/PRD",
                 "None/Other"
                 ))
    ) |> 
  # Grab comparisons we want
  filter(
    contrast %in% c("control - attack", "expert - attack", 
                    "partisan - attack", "domestic - attack", 
                    "domestic - attack", "expert - control") 
    ) |>
    # Arrange
  arrange(support, contrast) |> 
  # Select columns 
   select(study,
           Comparison = contrast,
          "Party Support" = support,
          Estimate = estimate,
         "Z-Statistic" = statistic,
         "Two-Tailed" = p.value,
         "Lower Tail" = lower_tail,
         "Upper Tail" =  upper_tail)

# Print  Study 1
all_party_comparisons |> 
  filter(study == "1") |> 
  select(-study) |> 
  xtable(digits = 2) |> 
  print(include.rownames = FALSE, 
        file = "tables/comparisons_inter_study1.tex")

# Print by Study 2
all_party_comparisons |> 
  filter(study == "2") |> 
  select(-study) |> 
  xtable(digits = 2) |> 
  print(include.rownames = FALSE, 
        file = "tables/comparisons_inter_study2.tex")


# Print Studies Combined
all_party_comparisons |> 
  filter(study == "combined", Comparison != "domestic - attack") |> 
  select(-study) |> 
  xtable(digits = 2) |> 
  print(include.rownames = FALSE, 
        file = "tables/comparisons_inter_combined.tex")

```


# Effect of Debrief

Test if the debrief (a variation of the expert rebuttal) mitigates the effect of the attack. We'll estimate within subjects models looking at how confidence in the INE changes between the first set of dependent variables and the same questions asked after the debrief.

```{r clean-for-debrief}


debrief_dat <- dat |> 
      # select vars
      select(conf_ine_t1, conf_ine_t2, 
             tx, trust_ine_pre, vote_intent, study) |> 
      # study 2 only
      filter(study == 2) |>
      # respondent rows
      mutate(respondent = row_number()) |> 
      pivot_longer(cols = c(conf_ine_t1, conf_ine_t2), 
                   names_to = "time", 
                   values_to = "conf_ine", 
                   names_pattern = "conf_ine_(.*)")

    
debrief_fit1 <- feols(conf_ine ~ time, cluster = ~respondent, 
                         subset(debrief_dat, tx == "attack"))    

debrief_fit2 <- feols(conf_ine ~ time * vote_intent, cluster = ~respondent, 
                         subset(debrief_dat, tx == "attack"))

# Get Predictions (Average Pre/Post by Party ID)
preds <- avg_predictions(debrief_fit2, variables = c("vote_intent", "time"),
                conf_level = .90)


preds <- preds |> 
  mutate(
    # Party Support
    support = case_match(vote_intent,
                           "Claudia" ~ "Morena",
                           "None/Other" ~ "None/Other",
                           "Xochitl" ~ "PAN/PRI/PRD"),
    # Time
    time = case_match(time, 
                      "t1" ~ "Post Attack",
                      "t2" ~ "Post Debrief")
    
    )


# Plot Predictions
ggplot(preds, aes(x = support, 
                  y = estimate,
                  ymin = conf.low, 
                  ymax = conf.high, 
                  color = time, 
                  shape = time)) +
  geom_pointrange(position = position_dodge(width = .2)) +
  labs(y = "Mean Confidence in INE", x = "Party Support") +
  theme_pubclean() +
  theme(legend.position = "bottom", 
        legend.title = element_blank(),
        axis.text = element_text(color = "black"))


ggsave(
  file = "figures/debreif_preds.pdf",
  height = 4,
  width = 4
  
)


```

```{r debrief-table}


coef_list_deb <- list("(Intercept)" = "Intercept",
                  "timet2" = "Post-Debrief",
                  "vote_intentNone/Other" = "Party Support: None/Other",
                  "vote_intentXochitl"  = "Party Support: PAN/PRI/PRD",
                  "timet2:vote_intentNone/Other" = "None/Other x Post-Debrief",
                  "timet2:vote_intentXochitl" = "PAN/PRI/PRD x Post-Debrief"
                )

cap_deb <- "Within Subject Models of Confidence in the INE."


texreg(list(debrief_fit1, debrief_fit2),
       custom.coef.map = coef_list_deb,
       stars = c(0.1, 0.05, 0.01),
       caption = cap_deb,
       caption.above = T,
       dcolumn = T,
       file = "tables/debrief_mods.tex",
       label = "si:post_deb_mods")



```

